﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities = Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Acls;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Agentes
{
    public partial class AgentesViewModel
    {
        #region Private properties

        /// <summary>
        /// 
        /// </summary>
        private List<Entities.Acl> _acls;

        /// <summary>
        /// 
        /// </summary>
        private Entities.Acl _acl;

        #endregion

        #region Public properties

        /// <summary>
        /// 
        /// </summary>
        public List<Entities.Acl> Acls
        {
            get
            {
                return _acls;
            }
            private set
            {
                _acls = value;
                if (_acls != null)
                    RaisePropertyChanged("Acls");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Entities.Acl Acl
        {
            get
            {
                return _acl;
            }
            private set
            {
                _acl = value;
                if (_acl != null)
                    RaisePropertyChanged("Acl");
            }
        }

        #endregion

        #region Actions

        /// <summary>
        /// 
        /// </summary>
        private void LoadAcls()
        {
            using (BackgroundWorker worker = new BackgroundWorker())
            {
                worker.DoWork += (sender, e) =>
                {
                    IAclService aclService = IoCFactory.Resolve<IAclService>();
                    e.Result = aclService.FindAllAcl();
                };
                worker.RunWorkerCompleted += (sender, e) =>
                {
                    if (!e.Cancelled && e.Error == null)
                        Acls = e.Result as List<Entities.Acl>;
                    else if (e.Error != null)
                        MessageBox.Show(
                            _agenteView,
                            e.Error.Message,
                            WindowTitles.AgentesWindowTitle,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                        );
                };
                worker.WorkerSupportsCancellation = true;
                worker.RunWorkerAsync();
            }
        }

        #endregion

        #region Event methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetCurrentAcl(object sender, EventArgs e)
        {
            BindingSource bindingSource = sender as BindingSource;
            if (bindingSource != null)
                if (bindingSource.Current != null)
                    Acl = bindingSource.Current as Entities.Acl;
        }

        /// <summary>
        /// 
        /// </summary>
        private void OnAclsPropertyChanged()
        {
            _agenteView.aclBindingSource.DataSource = Acls;
            _agenteView.aclBindingSource.CurrentChanged += SetCurrentAcl;
            SetCurrentAcl(_agenteView.aclBindingSource, new EventArgs());
        }

        /// <summary>
        /// 
        /// </summary>
        private void OnAclPropertyChanged()
        {
            if (IsAgenteReady)
                Agente.AclId = Acl.AclId;
        }

        #endregion
    }
}
